package com.itheima.health.service.impl;

import com.itheima.health.dao.UserDao;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.User;
import com.itheima.health.pojo.Users;
import com.itheima.health.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author zhangmeng
 * @description 用户服务实现类
 * @date 2019/9/6
 **/
@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserDao userDao;
    @Override
    public User findByUsername(String username) {
        //调用DAO查询数据
        return userDao.selectByUsername(username);
    }


    @Override
    public User login(Users user) {
        String code = (String) redisTemplate.opsForValue().get("MOBILE_SIGNIN" + "_" +user.getTelephone());
        if (code==null|!code.equals(user.getValidateCode())){
            throw new BusinessRuntimeException("验证码错误");
        }
        //查询用户
        Integer size =this.userDao.getByTelephone(user.getTelephone());
        if (size<1){
            User users = new User();
            users.setTelephone(user.getTelephone());
            users.setPassword("1234");
            users.setUsername("用户："+user.getTelephone());
            this.userDao.Insert(users);
            return users;
        }
       return this.userDao.getuser(user.getTelephone());
    }
}

